package com.siysoft.tc.admin.service.Impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.siysoft.tc.admin.service.IAdminUserManageService;
import com.siysoft.tc.base.bean.to.User;
import com.siysoft.util.IMap;
import com.tc.base.dao.BaseDAO;

/**
 * 方法摘要：实现后台管理员用户管理功能接口 作者：victoryu 日期：2015年10月22日
 * 
 * @param iMap
 * @return
 * @throws Exception
 */
@Service
public class AdminUserManageServiceImpl implements IAdminUserManageService {
	@Autowired
	BaseDAO baseDao;

	// 查询学生信息
	@Override
	public List<Map<String, String>> seekStudents(IMap iMap) throws Exception {
		String hql = "select * from db_user "
				+ "where user_type='0'";
		return baseDao.findBySQLToMap(hql, null, -1, -1);
	}

	// 根据学生ID跳转学生修改页面
	@Override
	public Map<String, String> toStuRevise(IMap iMap) throws Exception {
		String hql = "select * from db_user "
				+ "where user_type='0' and user_id = "
				+ iMap.getInt("user_id")
				+ " ";
		// 将查询结果存到list下
		List list = baseDao.findBySQLToMap(hql, iMap, -1, -1);
		// 判断list中是否存在数据
		if (list.size() > 0) {
			// list中存在数据返回list第一条数据
			return (Map<String, String>) list.get(0);
		} else {
			return null;
		}
	}

	// 修改学生基本信息
	@Override
	public void reviseStuInfo(IMap iMap) throws Exception {
		User us = (User)baseDao.findById(User.class, iMap.getInt("user_id"));
		String user_email = (String) iMap.get("user_email");
		us.setUser_email(user_email);
		String user_student_id = (String) iMap.get("user_student_id");
		us.setUser_student_id(user_student_id);
		String user_school = (String) iMap.get("user_school");
		us.setUser_school(user_school);
		String user_grade = (String) iMap.get("user_grade");
		us.setUser_grade(user_grade);
		int user_type = iMap.getInt("user_type");
		us.setUser_type(user_type);
		int user_lock = iMap.getInt("user_lock");
		us.setUser_lock(user_lock);
		baseDao.update(us);
	}
	
	//查询教师信息
	@Override
	public List<Map<String, String>> seekTeachers(IMap iMap) throws Exception {
/*		String hql = "select u.user_id,u.realname, u.email,"
				+ "u.telephone,u.username,u.user_money,u.channel, "
				+ " if(u.user_sex is null,0,u.user_sex) as user_sex,date_format(u.create_time,'%y-%m-%d %T') as create_time, if(u.teacher_status is null,0,u.teacher_status) as teacher_status "
				+ "from db_user u  where u.user_type='1' group by u.user_id order by u.teacher_status asc";*/
		
        String hql = "select * from db_user where user_type = '1' order by user_id asc";
//				+ "from db_user u left join db_teacher t on u.user_id = t.user_id "
//				+ "where u.user_type='1' group by u.user_id order by t.teacher_status asc";
		return baseDao.findBySQLToMap(hql, null, -1, -1);
	/*	+ "case "
		+ "when t.teacher_status='0' then '等待审核' "
		+ "when t.teacher_status='1' then '审核通过' "
		+ "when t.teacher_status='2' then '审核驳回' "
		+ "end teacher_status "*/
	}
	
	//查询教师信息
	@Override
	public List<Map<String, String>> seekRecommendTeachers(IMap iMap) throws Exception {
/*		String hql = "select u.user_id,u.realname, u.email,"
				+ "u.telephone,u.username,u.user_money,u.channel, "
				+ " if(u.user_sex is null,0,u.user_sex) as user_sex,date_format(u.create_time,'%y-%m-%d %T') as create_time, if(u.teacher_status is null,0,u.teacher_status) as teacher_status "
				+ "from db_user u  where u.user_type='1' group by u.user_id order by u.teacher_status asc";*/
		
        String hql = "select * from db_user where user_type = '1' and teacher_recommend = '1' order by user_id asc";
//					+ "from db_user u left join db_teacher t on u.user_id = t.user_id "
//					+ "where u.user_type='1' group by u.user_id order by t.teacher_status asc";
		return baseDao.findBySQLToMap(hql, null, -1, -1);
	/*	+ "case "
		+ "when t.teacher_status='0' then '等待审核' "
		+ "when t.teacher_status='1' then '审核通过' "
		+ "when t.teacher_status='2' then '审核驳回' "
		+ "end teacher_status "*/
	}
	
	//查询教师信息
	@Override
	public List<Map<String, String>> seekTeachersForSEM(IMap iMap) throws Exception {
		String hql = "select u.user_id,u.realname, u.email,"
				+ "u.telephone,u.username,u.user_money,u.channel, "
				+ " if(u.user_sex is null,0,u.user_sex) as user_sex,date_format(u.create_time,'%y-%m-%d %T') as create_time, if(u.teacher_status is null,0,u.teacher_status) as teacher_status "
				+ "from db_user u where u.user_type='1' ";
//				+ "from db_user u left join db_teacher t on u.user_id = t.user_id "
//				+ "where u.user_type='1' ";
		if(!Objects.equals("6", iMap.get("channel"))){
			hql = hql+" AND channel = "+iMap.get("channel") + " group by u.user_id order by t.teacher_status asc";
		}else{
			hql = hql + " group by u.user_id order by t.teacher_status asc";
		}
		
		return baseDao.findBySQLToMap(hql, null, -1, -1);
	}	
	
	//根据教师ID跳转教师修改页面
	@Override
	public Map<String, String> toTeaRevise(IMap iMap) throws Exception {
		
        String hql = "select * from db_user where user_type = '1' and user_id = " + iMap.getInt("user_id");
//				+ "from db_user u left join db_teacher t on u.user_id = t.user_id "
//				+ "where u.user_type='1' group by u.user_id order by t.teacher_status asc";
		List<Map<String, String>> list =  baseDao.findBySQLToMap(hql, null, -1, -1);
		// 判断list中是否存在数据
		if (list.size() > 0) {
			// list中存在数据返回list第一条数据
			return (Map<String, String>) list.get(0);
		} else {
			return null;
		}
	}
	
	//根据教师ID跳转教师修改页面
	@Override
	public Map<String, String> toRecommendTeaRevise(IMap iMap) throws Exception {
		
		String hql = "select * from db_user where user_type = '1' and teacher_recommend = '1' and user_id = " + iMap.getInt("user_id");
//				+ "from db_user u left join db_teacher t on u.user_id = t.user_id "
//				+ "where u.user_type='1' group by u.user_id order by t.teacher_status asc";
		List<Map<String, String>> list =  baseDao.findBySQLToMap(hql, null, -1, -1);
		// 判断list中是否存在数据
		if (list.size() > 0) {
			// list中存在数据返回list第一条数据
			return (Map<String, String>) list.get(0);
		} else {
			return null;
		}
	}
	
	
    //根据学生ID修改教师详情基本信息
    @Override
    public Map<String, String> toTeaDetail(IMap iMap) throws Exception {
        // TODO Auto-generated method stub
        String hql = "select * from db_user where user_id = " + iMap.getInt("user_id") + " ";
        // 将查询结果存到list下
        List list = baseDao.findBySQLToMap(hql, null, -1, -1);
        // 判断list中是否存在数据
        if (list.size() > 0) {
            // list中存在数据返回list第一条数据
            return (Map<String, String>) list.get(0);
        } else {
            return null;
        }
    }
	
	
	@Override
	public List<Map<String, String>> getResource(IMap iMap) throws Exception {
//		String hql = "SELECT * FROM (SELECT 209 AS type) a LEFT JOIN ( SELECT '个人持证照' AS NAME, a.img_path, a.data_type FROM db_resource a WHERE a.data_type = 209 AND a.user_id = "+iMap.getString("user_id")+" ) b ON a.type = b.data_type UNION SELECT * FROM (SELECT 201 AS type) a LEFT JOIN ( SELECT '身份证正面' AS NAME, a.img_path, a.data_type FROM db_resource a WHERE a.data_type = 201 AND a.user_id = "+iMap.getString("user_id")+" ) b ON a.type = b.data_type UNION SELECT * FROM (SELECT 202 AS type) a LEFT JOIN ( SELECT '身份证反面' AS NAME, a.img_path, a.data_type FROM db_resource a WHERE a.data_type = 202 AND a.user_id = "+iMap.getString("user_id")+" ) b ON a.type = b.data_type UNION SELECT * FROM (SELECT 203 AS type) a LEFT JOIN ( SELECT '教师资格证' AS NAME, a.img_path, a.data_type FROM db_resource a WHERE a.data_type = 203 AND a.user_id = "+iMap.getString("user_id")+" ) b ON a.type = b.data_type UNION SELECT * FROM (SELECT 204 AS type) a LEFT JOIN ( SELECT '学生证' AS NAME, a.img_path, a.data_type FROM db_resource a WHERE a.data_type = 204 AND a.user_id = "+iMap.getString("user_id")+" ) b ON a.type = b.data_type UNION SELECT * FROM (SELECT 205 AS type) a LEFT JOIN ( SELECT '毕业证' AS NAME, a.img_path, a.data_type FROM db_resource a WHERE a.data_type = 205 AND a.user_id = "+iMap.getString("user_id")+" ) b ON a.type = b.data_type UNION SELECT * FROM (SELECT 206 AS type) a LEFT JOIN ( SELECT '等级证书' AS NAME, a.img_path, a.data_type FROM db_resource a WHERE a.data_type = 206 AND a.user_id = "+iMap.getString("user_id")+" ) b ON a.type = b.data_type UNION SELECT * FROM (SELECT 207 AS type) a LEFT JOIN ( SELECT '获奖证书' AS NAME, a.img_path, a.data_type FROM db_resource a WHERE a.data_type = 207 AND a.user_id = "+iMap.getString("user_id")+" ) b ON a.type = b.data_type UNION SELECT * FROM (SELECT 1001 AS type) a LEFT JOIN ( SELECT '头像' AS NAME, a.img_path, a.data_type FROM db_resource a WHERE a.data_type = 1001 AND a.user_id = "+iMap.getString("user_id")+" ) b ON a.type = b.data_type";
		String hql = "SELECT * FROM (SELECT 209 AS type) a LEFT JOIN ( SELECT '个人持证照' AS NAME, a.img_path, a.data_type FROM db_resource a WHERE a.data_type = 209 AND a.user_id = "+iMap.getString("user_id")+" ) b ON a.type = b.data_type UNION"
                   +" SELECT * FROM (SELECT 201 AS type) a LEFT JOIN ( SELECT '身份证正面' AS NAME, a.img_path, a.data_type FROM db_resource a WHERE a.data_type = 201 AND a.user_id = "+iMap.getString("user_id")+" ) b ON a.type = b.data_type UNION"
				   +" SELECT * FROM (SELECT 202 AS type) a LEFT JOIN ( SELECT '身份证反面' AS NAME, a.img_path, a.data_type FROM db_resource a WHERE a.data_type = 202 AND a.user_id = "+iMap.getString("user_id")+" ) b ON a.type = b.data_type UNION"
                   +" SELECT * FROM (SELECT 203 AS type) a LEFT JOIN ( SELECT '教师资格证' AS NAME, a.img_path, a.data_type FROM db_resource a WHERE a.data_type = 203 AND a.user_id = "+iMap.getString("user_id")+" ) b ON a.type = b.data_type UNION"
				   +" SELECT * FROM (SELECT 210 AS type) a LEFT JOIN ( SELECT '学历认证' AS NAME, a.img_path, a.data_type FROM db_resource a WHERE a.data_type = 210 AND a.user_id = "+iMap.getString("user_id")+" ) b ON a.type = b.data_type UNION"
                   +" SELECT * FROM (SELECT 211 AS type) a LEFT JOIN ( SELECT '资质认证' AS NAME, a.img_path, a.data_type FROM db_resource a WHERE a.data_type = 211 AND a.user_id = "+iMap.getString("user_id")+" ) b ON a.type = b.data_type ";
		
		return baseDao.findBySQLToMap(hql,null,0,-1);
		
	}

	@Override
	public Map<String, String> toTeaDelete(IMap iMap) throws Exception {
		Map<String,String> map = new HashMap<String,String>(); 
		try{
//			String delTeaSql = "delete from Teacher where userId ="+iMap.getInt("user_id");
//			baseDao.deleteByHQL(delTeaSql);
			String delUserSql = "delete from User where userId ="+iMap.getInt("user_id");
			baseDao.deleteByHQL(delUserSql);
			map.put("success", "true");
		}catch(Exception ex){
			map.put("success", "false");
		}
		
		return map;
	}
	@Override
	public void teaRevise(IMap iMap) throws Exception {
		// TODO Auto-generated method stub
		User us = (User)baseDao.findById(User.class, iMap.getInt("user_id"));
		String teacher_course = (String) iMap.get("teacher_course");
		us.setTeacher_course(teacher_course);
		String teacher_department = (String) iMap.get("teacher_department");
		us.setTeacher_department(teacher_department);
		String teacher_experience = (String) iMap.get("teacher_experience");
		us.setTeacher_experience(teacher_experience);
		Integer teacher_goal = iMap.getInt("teacher_goal");
		us.setTeacher_goal(teacher_goal);
		String teacher_introduce = (String) iMap.get("teacher_introduce");
		us.setTeacher_introduce(teacher_introduce);
		String teacher_level = (String) iMap.get("teacher_level");
		us.setTeacher_level(teacher_level);
		Integer teacher_mr = (Integer) iMap.getInt("teacher_mr");
		us.setTeacher_mr(teacher_mr);
		Integer teacher_pt = (Integer) iMap.getInt("teacher_pt");
		us.setTeacher_pt(teacher_pt);
		Integer teacher_wage = (Integer) iMap.getInt("teacher_wage");
		us.setTeacher_wage(teacher_wage);
		String user_email = (String) iMap.get("user_email");
		us.setUser_email(user_email);
		String user_grade = (String) iMap.get("user_grade");
		us.setUser_grade(user_grade);
		Integer user_recommend_index = (Integer) iMap.getInt("user_recommend_index");
		us.setUser_recommend_index(user_recommend_index);
		String user_school = (String) iMap.get("user_school");
		us.setUser_school(user_school);
		String user_student_id = (String) iMap.get("user_student_id");
		us.setUser_student_id(user_student_id);
		Integer teacher_pt_mr_type = (Integer) iMap.getInt("teacher_pt_mr_type");
		us.setTeacher_pt_mr_type(teacher_pt_mr_type);
		Integer teacher_level_type = (Integer) iMap.getInt("teacher_level_type");
		us.setTeacher_level_type(teacher_level_type);
		Integer user_lock = (Integer) iMap.getInt("user_lock");
		us.setUser_lock(user_lock);
		Integer teacher_recommend = (Integer) iMap.getInt("teacher_recommend");
		us.setTeacher_recommend(teacher_recommend);	
		String teacher_remarks = (String) iMap.get("teacher_remarks");
		us.setTeacher_remarks(teacher_remarks);
		baseDao.save(us);
		
	}

}
